home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / inherit3.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  3KB  |  123 lines

  1.                                   // Chapter 8 - Program 3
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5. protected:
  6.    int wheels;
  7.    float weight;
  8. public:
  9.    void initialize(int in_wheels, float in_weight);
  10.    int get_wheels(void) {return wheels;}
  11.    float get_weight(void) {return weight;}
  12.    float wheel_loading(void) {return weight/wheels;}
  13. };
  14.  
  15.  
  16.  
  17.  
  18. class car : vehicle {
  19.    int passenger_load;
  20. public:
  21.    void initialize(int in_wheels, float in_weight, int people = 4);
  22.    int passengers(void) {return passenger_load;}
  23. };
  24.  
  25.  
  26.  
  27.  
  28. class truck : vehicle {
  29.    int passenger_load;
  30.    float payload;
  31. public:
  32.    void init_truck(int in_wheels, float in_weight,
  33.                           int how_many = 2, float max_load = 24000.0);
  34.    float efficiency(void);
  35.    int passengers(void) {return passenger_load;}
  36. };
  37.  
  38.  
  39.  
  40.  
  41. main()
  42. {
  43. vehicle unicycle;
  44.  
  45.    unicycle.initialize(1, 12.5);
  46.    cout << "The unicycle has " <<
  47.                                 unicycle.get_wheels() << " wheel.\n";
  48.    cout << "The unicycle's wheel loading is " <<
  49.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  50.    cout << "The unicycle weighs " <<
  51.                              unicycle.get_weight() << " pounds.\n\n";
  52.  
  53. car sedan;
  54.  
  55.    sedan.initialize(4, 3500.0, 5);
  56.    cout << "The sedan carries " << sedan.passengers() <<
  57.                                                     " passengers.\n";
  58. // cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  59. // cout << "The sedan's wheel loading is " <<
  60. //                  sedan.wheel_loading() << " pounds per tire.\n\n";
  61.  
  62. truck semi;
  63.  
  64. // semi.initialize(18, 12500.0);
  65.    semi.init_truck(1, 33675.0);
  66. // cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  67.    cout << "The semi's efficiency is " <<
  68.                           100.0 * semi.efficiency() << " percent.\n";
  69. }
  70.  
  71.  
  72.  
  73.  
  74.               // initialize to any data desired
  75. void
  76. vehicle::initialize(int in_wheels, float in_weight)
  77. {
  78.    wheels = in_wheels;
  79.    weight = in_weight;
  80. }
  81.  
  82.  
  83.  
  84. void
  85. car::initialize(int in_wheels, float in_weight, int people)
  86. {
  87.    passenger_load = people;
  88.    wheels = in_wheels;
  89.    weight = in_weight;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. void
  96. truck::init_truck(int in_wheels, float in_weight,
  97.                                         int how_many, float max_load)
  98. {
  99.    vehicle::initialize(in_wheels, in_weight);
  100.    passenger_load = how_many;
  101.    payload = max_load;
  102. }
  103.  
  104.  
  105.  
  106. float
  107. truck::efficiency(void)
  108. {
  109.    return payload / (payload + weight);
  110. }
  111.  
  112.  
  113.  
  114.  
  115. // Result of execution
  116. //
  117. // The unicycle has 1 wheel.
  118. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  119. // The unicycle weighs 12.5 pounds.
  120. //
  121. // The sedan carries 5 passengers.
  122. // The semi's efficiency is 41.612484 percent.
  123.